package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.union.UnionactionMain;
import com.motu.monstercity.server.game.commondata.union.UnionactionReward;
import com.motu.monstercity.server.game.commondata.union.UnionactionTaskType;
import com.motu.monstercity.server.game.commondata.worldboss.WorldBossReward;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class UnionMobileBaseData {
    private static Logger logger = LoggerFactory.getLogger(UnionMobileBaseData.class);

    public static void initBaseData(List<Future> futures) {
        initUnionActionTaskType();
        initUnionactionMain();
        initUnionactionReward();
    }


    private static ConcurrentHashMap<Integer, UnionactionTaskType> unionActionTaskTypeMap = new ConcurrentHashMap<>(); // 联盟总动员任务类型表  type->data
    private static ConcurrentHashMap<Integer, UnionactionMain> unionActionMainMap = new ConcurrentHashMap<>(); // 联盟总动员任务总表
    private static CopyOnWriteArrayList<UnionactionReward> unionactionRewards = new CopyOnWriteArrayList<>(); // 联盟总动员任务总表

    /**
     * 初始化联盟总动员任务类型表
     */
    public static Future<Void> initUnionActionTaskType() {
        String table = "unionaction_task_type";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, UnionactionTaskType> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        UnionactionTaskType entity = new UnionactionTaskType(result.getJsonObject(i));
                        tmp.put(entity.getType(), entity);
                    }
                    unionActionTaskTypeMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static UnionactionTaskType getUnionactionTaskType(int type) {
        UnionactionTaskType result = unionActionTaskTypeMap.get(type);
        if (result == null) {
            logger.error("getUnionactionTaskType error key = " + type);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, UnionactionTaskType> getUnionactionTaskTypeMap() { return unionActionTaskTypeMap; }



    /**
     * 初始化联盟总动员任务总表
     */
    public static Future<Void> initUnionactionMain() {
        String table = "unionaction_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, UnionactionMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        UnionactionMain entity = new UnionactionMain(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    unionActionMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static UnionactionMain getUnionactionMain(int id) {
        UnionactionMain result = unionActionMainMap.get(id);
        if (result == null) {
            logger.error("getUnionactionMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, UnionactionMain> getUnionactionMainMap() { return unionActionMainMap; }




    /**
     * 初始化联盟总动员里程奖励
     */
    public static Future<Void> initUnionactionReward() {
        String table = "unionaction_reward";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    CopyOnWriteArrayList<UnionactionReward> tmp = new CopyOnWriteArrayList<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        UnionactionReward entity = new UnionactionReward(result.getJsonObject(i));
                        tmp.add(entity);
                    }
                    tmp.sort(Comparator.comparingInt(UnionactionReward::getUnionScore)); // 按照升序先排列
                    unionactionRewards = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }


    public static CopyOnWriteArrayList<UnionactionReward> getUnionactionRewards() {
        return unionactionRewards;
    }
}
